#include "RTL.h"


bool RTL::lastHasData = true;
vector<RTL*> RTL::quaternaries;
void RTL::putData(Token* op, Base* addr1, Base* addr2, Base* des)
{
	if (lastHasData)
	{
		auto m = new RTL();
		m->op = op;
		m->Addr1 = addr1;
		m->Addr2 = addr2;
		m->Addr3 = des;
		quaternaries.push_back(m);
	}
	else
	{
		auto m = quaternaries[quaternaries.size() - 1];
		m->op = op;
		m->Addr1 = addr1;
		m->Addr2 = addr2;
		m->Addr3 = des;
		lastHasData = true;
	}
}

void RTL::putLabel(Label * label)
{
	if (lastHasData)
	{
		auto m = new RTL();
		m->labels->push_back(label);
		quaternaries.push_back(m);
		lastHasData = false;
	}
	else
	{
		auto m = quaternaries[quaternaries.size() - 1];
		m->labels->push_back(label);
		quaternaries.push_back(m);
	}
}

void RTL::Output()
{
	for (size_t i = 0; i < quaternaries.size(); i++)
	{
		for (size_t j = 0; j < quaternaries[i]->labels->size(); j++)
		{
			cout << quaternaries[i]->labels->at(j)->ToRTL() << ',';
		}cout << ":\t";
		if (quaternaries[i]->op)
			cout << quaternaries[i]->op->toRTL() << '\t';
		else
			cout << "nil\t";
		if (quaternaries[i]->Addr1)
			cout << quaternaries[i]->Addr1->ToRTL() << '\t';
		else
			cout << "nil\t";
		if (quaternaries[i]->Addr2)
			cout << quaternaries[i]->Addr2->ToRTL() << '\t';
		else
			cout << "nil\t";
		if (quaternaries[i]->Addr3)
			cout << quaternaries[i]->Addr3->ToRTL() << endl;
		else
			cout << "nil\n";
	}
}

void RTL::Init()
{
	quaternaries.clear();
	lastHasData = true;

}

RTL::RTL()
{
	labels = new vector<Label*>();
}


RTL::~RTL()
{
}
